'use strict';

import axios from 'axios';
import qs from 'qs';

const baseURL = 'http://foodapi.jimanke.com/api';
// const baseURL = 'http://www.tzpays.test/api';

function checkStatus (response) {
  // 判断token是否有效
  if (response.data.code + '' === '30001' || response.data.code + '' === '30002' || response.data.code + '' === '30003' || response.data.code + '' === '30004') {
    // token无效
    var str = location.protocol + '//' + location.hostname + ':' + location.port;
    localStorage.setItem('token', '');
    location.replace(str);
    return {
      status: 404,
      msg: response.data.msg
    };
  }
  // 如果http状态码正常，则直接返回数据
  if (
    response &&
    (response.status === 200 ||
      response.status === 256 ||
      response.status === 304 ||
      response.status === 400)
  ) {
    return response.data;
    // 如果不需要除了data之外的数据，可以直接 return response.data
  }
  // 异常状态下，把错误信息返回去
  return {
    status: 404,
    msg: '网络异常'
  };
}


function checkCode (response) {
  console.log(response);
  if (
    response &&
    (response.status === 200 ||
      response.status === 256 ||
      response.status === 304 ||
      response.status === 400)
  ) {
    return response.data;
    // 如果不需要除了data之外的数据，可以直接 return response.data
  }
  // 异常状态下，把错误信息返回去
  return {
    status: 404,
    msg: '网络异常'
  };
}

export default {
  
  // 图片上传
  upImg (url, data, success, fail) {
    let formdata = new FormData();
    formdata.append('image', data.file);
    axios({
      url: baseURL + url,
      method: 'post',
      data: formdata,
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
      .then(response => {
        return success(checkStatus(response));
      })
      .catch(res => {
        return fail(checkCode(res));
      });
  },
  // 文件上传 @data是一个对象
  upFile (url, data, success, fail) {
    let formdata = new FormData();
    // 循环参数
    for(var key in data) {
      // console.log(dataObj[key])
      formdata.append(key, data[key]);
    }
    
    axios({
      url: baseURL + url,
      method: 'post',
      data: formdata,
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
      .then(response => {
        return success(checkStatus(response));
      })
      .catch(res => {
        return fail(checkCode(res));
      });
  },
  // post请求
  post (url, data, success, fail) {
    axios
      .post(baseURL + url, qs.stringify(data))
      .then(function (response) {
        return success(checkStatus(response));
      })
      .catch(function (res) {
        return fail(checkCode(res));
      });
  },
  // get请求
  get (url, data, success, fail) {
    axios
      .get(baseURL + url, {
        params: data
      })
      .then(function (response) {
        return success(checkStatus(response));
      })
      .catch(function (res) {
        console.log(res);
        return fail(checkCode(res));
      });
  },
  form (url, data, config, success, fail) {
    axios
      .post(baseURL + url, data, config)
      .then(function (response) {
        return success(checkStatus(response));
      })
      .catch(function (res) {
        return fail(checkCode(res));
      });
  }
};
